'''
QQ登录
获取权限
发送消息
浏览朋友圈
'''
from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
import time

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0.1'
desired_caps['deviceName'] = '127.0.0.1:7555'
desired_caps['appPackage'] = 'com.tencent.mobileqq'
desired_caps['appActivity'] = '.activity.LoginActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

driver.implicitly_wait(20)  #隐式等待20秒
driver.find_element_by_android_uiautomator('new UiSelector().text("登录")').click()   #点击登录按钮
driver.find_element_by_android_uiautomator('new UiSelector().text("QQ号/手机号/邮箱")').click()   #点击账号输入框

#=================输入账号==========
driver.keyevent(11)
driver.keyevent(7)
driver.keyevent(12)
driver.keyevent(13)
driver.keyevent(13)
driver.keyevent(13)
driver.keyevent(12)
driver.keyevent(14)
driver.keyevent(15)

# driver.find_element_by_android_uiautomator('new UiSelector().text("QQ号/手机号/邮箱")').send_keys("405666578")
#输入密码
driver.find_element_by_android_uiautomator('new UiSelector().description("密码 安全")').send_keys("bi19940604")
#点击登录
driver.find_element_by_android_uiautomator('new UiSelector().description("登 录")').click()
#点击同意协议
driver.find_element_by_android_uiautomator('new UiSelector().description("同意")').click()
#获取电话权限
driver.find_element_by_android_uiautomator('new UiSelector().description("去授权按钮")').click()
#获取图片、媒体内容和文件权限
driver.find_element_by_android_uiautomator('new UiSelector().text("允许")').click()
#获取拨打电话和管理通话权限
driver.find_element_by_android_uiautomator('new UiSelector().text("允许")').click()
#点击登录
driver.find_element_by_android_uiautomator('new UiSelector().description("登 录")').click()
#不更新版本
driver.find_element_by_android_uiautomator('new UiSelector().text("稍后处理")').click()
#获取当前可用上下文
ct = driver.contexts
#打印当前可用上下文
print(ct)
#获取全部好友
name = driver.find_element_by_id("com.tencent.mobileqq:id/root").find_element_by_id("com.tencent.mobileqq:id/ec3").find_elements_by_class_name("android.widget.LinearLayout")
#选中好友并进入编辑页面
name[5].click()

#===================给何尧发信息
driver.find_element_by_id("com.tencent.mobileqq:id/input").send_keys("小伙子")
driver.find_element_by_android_uiautomator('new UiSelector().text("发送")').click()
driver.find_element_by_id("com.tencent.mobileqq:id/emo_btn").click()
driver.find_element_by_android_uiautomator('new UiSelector().description("爆筋")').click()
driver.find_element_by_android_uiautomator('new UiSelector().text("发送")').click()
driver.find_element_by_android_uiautomator('new UiSelector().description("返回消息")').click()

#====================给张相伟发信息=================
zi = driver.find_element_by_id("com.tencent.mobileqq:id/recent_chat_list")
zi.click()
driver.find_element_by_id("com.tencent.mobileqq:id/input").send_keys("小伙")
driver.find_element_by_android_uiautomator('new UiSelector().text("发送")').click()
driver.find_element_by_android_uiautomator('new UiSelector().description("返回消息")').click()


#点击动态
driver.find_element_by_android_uiautomator('new UiSelector().text("动态")').click()
#点击好友动态
driver.find_element_by_android_uiautomator('new UiSelector().text("好友动态")').click()

#获取当前应用的activity
ct = driver.current_activity
#打印当前应用的activity
print(ct)
#睡眠1秒
time.sleep(1)

#上划，浏览朋友圈
driver.swipe(600,1300,600,90,1000)
driver.swipe(600,1300,600,90,1000)
driver.swipe(600,1300,600,90,1000)
driver.swipe(600,1300,600,90,1000)
driver.swipe(600,1300,600,90,1000)

#退出
driver.quit()
